package com.yanqu.road.dataHelper.action.excel;

import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.manager.DataMgr;
import com.yanqu.road.dataHelper.repair.BaseRepair;
import com.yanqu.road.entity.player.UserInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author xx
 * @Date 2020/11/11
 */
public class HalloweenLog extends BaseRepair {

    public HalloweenLog(GameServer gameServer) {
        super(gameServer);
    }

    @Override
    public void repair(long param1, String param2) {

    }

    public void go1(String startDate ,String endDate){
        List<Map<String, String>> listMap = new ArrayList<>();
        listMap = getLog1(startDate,endDate);
        DataMgr.addData(listMap);
    }

    public void go2(String startDate ,String endDate){
        List<Map<String, String>> listMap = new ArrayList<>();
        listMap = getLog2(startDate,endDate);
        DataMgr.addData(listMap);
    }

    public void go3(String startDate ,String endDate){
        List<Map<String, String>> listMap = new ArrayList<>();
        listMap = getLog3(startDate,endDate);
        DataMgr.addData(listMap);
    }

    public void go4(String startDate ,String endDate){
        List<Map<String, String>> listMap = new ArrayList<>();
        listMap = getLog4(startDate,endDate);
        DataMgr.addData(listMap);
    }

    //使用灵猫拳
    private List<Map<String, String>> getLog1(String startDate ,String endDate) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * " +
                " from t_l_goods " +
                " where goodsId = 120060 and isGet = 0 and `time` > ? and `time` < ?";
        try {
            call = getGameServer().getLogConnection().prepareCall(sql);
            int i=1;
            call.setString(i++, startDate);
            call.setString(i++, endDate);
            rs = call.executeQuery();
            while (rs.next()) {
                Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                temp.put("userId",checkNullToEmpty(rs.getString("userId")));
                UserInfo userInfo = getUserInfo(temp.get("userId"));
                temp.put("openId",userInfo.getOpenId());
                temp.put("使用数量",checkNullToEmpty(rs.getString("num")));
                temp.put("使用时间",checkNullToEmpty(rs.getString("time")));
                dataList.add(temp);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataList;
    }

    //获得灵猫卷
    private List<Map<String, String>> getLog2(String startDate ,String endDate) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * " +
                " from t_l_goods " +
                " where goodsId = 120060 and isGet = 1 and `time` > ? and `time` < ?";
        try {
            call = getGameServer().getLogConnection().prepareCall(sql);
            int i=1;
            call.setString(i++, startDate);
            call.setString(i++, endDate);
            rs = call.executeQuery();
            while (rs.next()) {
                Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                temp.put("userId",checkNullToEmpty(rs.getString("userId")));
                UserInfo userInfo = getUserInfo(temp.get("userId"));
                temp.put("openId",userInfo.getOpenId());
                temp.put("获取召唤券数量",checkNullToEmpty(rs.getString("num")));
                temp.put("获取系统",checkNullToEmpty(rs.getString("way")));
                temp.put("获取时间",checkNullToEmpty(rs.getString("time")));
                temp.put("vipExp",String.valueOf(userInfo.getVipExp()));
                dataList.add(temp);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataList;
    }

    private List<Map<String, String>> getLog3(String startDate ,String endDate) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * " +
                " from t_l_activity_remain_time " +
                " where activityType = 22 and `time` > ? and `time` < ?";
        try {
            call = getGameServer().getLogConnection().prepareCall(sql);
            int i=1;
            call.setString(i++, startDate);
            call.setString(i++, endDate);
            rs = call.executeQuery();
            while (rs.next()) {
                Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                temp.put("userId",checkNullToEmpty(rs.getString("userId")));
                UserInfo userInfo = getUserInfo(temp.get("userId"));
                temp.put("openId",userInfo.getOpenId());
                temp.put("activityId",checkNullToEmpty(rs.getString("activityId")));
                temp.put("停留秒",checkNullToEmpty(rs.getString("remainTime")));
                temp.put("开始时间",checkNullToEmpty(rs.getString("time")));
                dataList.add(temp);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataList;
    }

    //获得灵猫卷
    private List<Map<String, String>> getLog4(String startDate ,String endDate) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * " +
                " from t_u_holiday_activity_data ";
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            int i=1;
            rs = call.executeQuery();
            while (rs.next()) {
                Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                temp.put("userId",checkNullToEmpty(rs.getString("userId")));
                UserInfo userInfo = getUserInfo(temp.get("userId"));
                temp.put("openId",userInfo.getOpenId());
                temp.put("activityId",checkNullToEmpty(rs.getString("activityId")));
                temp.put("南瓜个数",checkNullToEmpty(rs.getString("progressVal")));
                temp.put("vipExp",String.valueOf(userInfo.getVipExp()));
                dataList.add(temp);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataList;
    }


}
